var MongoClient = require('mongodb').MongoClient;

var async = require('async');

var driver = require('./db');

var response = require('./response');

const Url = "mongodb://localhost:27017/manhua";

const table = 'manhua';


var manhuaDao = {};
/**
 * 
 * @param {*name ,type} param 
 * @param {*} res 
 */
manhuaDao.search = function (param, res) {
    var _query = {};

    if (param.name) {
        _query.title = { '$regex': param.name, '$options': 'i' };

    }
    // // isFinish
    // if (param.isFinish && param.isFinish!=='') {

    //     _query.isFinish = (param.isFinish == 'true') ? true : false;
    // }

    // // type
    // if (param.type && param.type!=='null') {
    //     _query.type = param.type == '""'?"":param.type;
    // }

    console.log(_query);



    async.waterfall([
        // 连接数据库        
        function (callback) {
            driver.connect(callback, Url);
        },
        // 超找
        function (db, callback) {

            driver.find(db, callback, table, _query, {});
        },
        function (db, res, callback) {
            driver.close(db, res, callback);
        }],
        // result
        function (err, result) {
            console.log("resulte");

            if (err) {
                res.json(err);
                return;
            }
            res.json(result);
        }
    );
};

/**
 * 
 * @param {*size lastid type} param 
 * @param {*} res 
 */
manhuaDao.page = function (param, res) {
    var _query = {};

    try {
        param = JSON.parse(param);
    } catch (e) { }

    console.log('--x-x-x-');
    console.log(param);
    // isFinish
    if (param.isFinish && param.isFinish !== 'all') {
        _query.isFinish = !!JSON.parse(String(param.isFinish).toLowerCase());
    }

    // type
    if (typeof (param.type) !== "undefined" && param.type !== 'all') {
        _query.type = param.type;
    }

    // letter
    if (param.char && param.char !== 'all') {
        _query.letter = param.char;
    }

    // website
    if (param.website && param.website !== 'all') {
        var domain;
        switch (param.website) {
            case '1kkk':
                domain = 'www.1kkk.com';
                break;
            case 'dmzj':
                domain = 'manhua.dmzj.com';
                break;
            case 'kuku':
                domain = 'comic.kukudm.com';
                break;
            default: break;
        }



        _query.domain = domain;
    }

    // search
    if (param.search) {
        // _query.title = { '$regex': param.search, '$options': 'i' };

        _query['$or'] = [
            { title: { '$regex': param.search, '$options': 'i' } },
            { auth: { '$regex': param.search, '$options': 'i' } }
        ];
    }


    console.log('-------query-------');
    console.log(_query);

    var page_size = Number(param.size) || 10;

    var page_cur = Number(param.curPage) || 1;



    var _sort = {};

    if (param.update && param.update != 'all') {
        _sort = { "isUpdate": -1, 'updateTime': -1 };
    } else {
        _sort = { 'updateTime': -1 };
    }

    // 返回参数
    var backQuery = {
        chapters: 0,
        info: 0,
        isFinish: 0,
        spiderTime: 0
    };

    async.waterfall([
        // 连接数据库        
        function (callback) {
            driver.connect(callback, Url);
        },
        // 分页
        function (db, callback) {
            driver.page(db, callback, table, _query, backQuery, page_cur, page_size, _sort);
        },
        function (db, res, callback) {
            driver.close(db, res, callback);
        }
    ],
        // result
        function (err, result) {


            if (err) {
                res.json(err);
                return;
            }
            res.json(result);
        }
    );

};

// manhua detail
manhuaDao.find = function (param, res) {

    var _query = {};

    if (param['_id']) {
        _query['_id'] = require('mongodb').ObjectID(param['_id']);
    }


    async.waterfall([
        // 连接数据库        
        function (callback) {
            driver.connect(callback, Url);
        },
        // 超找
        function (db, callback) {

            driver.find(db, callback, table, _query, {});
        },
        function (db, res, callback) {
            driver.close(db, res, callback);
        }],
        // result
        function (err, result) {
            console.log("resulte");

            if (err) {
                res.json(err);
                return;
            }
            res.json(result);
        }
    );
};

// 漫画是否更新
manhuaDao.isUpdate = function (param, res) {
    var _query = {};

    if (param['_id']) {
        _query['_id'] = require('mongodb').ObjectID(param['_id']);
    } else {
        res.json({ success: false, msg: '_id is miss' });
    }


    async.waterfall([
        // 连接数据库        
        function (callback) {
            driver.connect(callback, Url);
        },
        // 超找
        function (db, callback) {

            driver.find(db, callback, table, _query, { chapters: 0, spiderTime: 0 });
        },
        function (db, res, callback) {
            driver.close(db, res, callback);
        }],
        // result
        function (err, result) {
            console.log("resulte");

            if (err) {
                res.json(err);
                return;
            }
            res.json(result);
        }
    );
};
module.exports = manhuaDao;